package com.neo.sk.util.http;

import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * User: Huangshanqi
 * Date: 2015/7/13
 * Time: 12:41
 */
public class DoubanLogin {

  public static CookieStore Mylogin() {
    System.out.println("----------------------------Mylogin");
    try {
      Header[] headers = new Header[9];
      CloseableHttpClient httpclient = HttpClients.createDefault();

      /****************************************************/
      //http://www.newsmth.net/nForum/user/ajax_login.json
      headers[0] = (new BasicHeader("User-Agent", HttpContants.defaultUserAgent));
      headers[1] = (new BasicHeader("Accept", HttpContants.defaultAccept));
      headers[2] = (new BasicHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"));
      headers[3] = (new BasicHeader("Connection", "keep-alive"));
      headers[4] = (new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"));
      headers[5] = (new BasicHeader("Host", "www.douban.com"));
      headers[6] = (new BasicHeader("Pragma", "no-cache"));
      headers[7] = (new BasicHeader("Referer", "http://www.douban.com/"));
      headers[8] = (new BasicHeader("X-Requested-With", "XMLHttpRequest"));

      HttpPost httppost = new HttpPost("https://accounts.douban.com/login");

      List<NameValuePair> formparams = new ArrayList<NameValuePair>();
      formparams.add(new BasicNameValuePair("source", ""));
      formparams.add(new BasicNameValuePair("redir", ""));
      formparams.add(new BasicNameValuePair("form_email", "844296617@qq.com"));
      formparams.add(new BasicNameValuePair("form_password", "0310huangshanqi"));
      formparams.add(new BasicNameValuePair("remember", "on"));
      formparams.add(new BasicNameValuePair("login", "登录"));
      UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);

      CookieStore cookieStore = null;
      HttpClientContext context = HttpClientContext.create();
      context.setCookieStore(cookieStore);

      httppost.setEntity(entity);
//      httppost.setHeaders(headers);
/*      String tempProxy = ProxyManager.getManager().getProxy();
      while (tempProxy == null)
        tempProxy =  ProxyManager.getManager().getProxy();
      System.out.println("tempProxy==========================="+tempProxy);
      String[] list = tempProxy.split(":");
      if(list.length == 2){
        HttpHost host = new HttpHost(list[0], Integer.parseInt(list[1]));
        RequestConfig config = RequestConfig.custom()
            .setProxy(host)
            .build();
        httppost.setConfig(config);
      }else{
        System.out.println("login proxy error==========="+ tempProxy);
      }*/
      //httppost.setConfig(RequestConfig.custom().setProxy(new HttpHost("127.0.0.1",8087)).build());
      HttpResponse response = httpclient.execute(httppost, context);
      String str = EntityUtils.toString(response.getEntity(), "utf-8");
      System.out.print(">>>>>>>>>>>>>>>>\n"+str + "<<<<<<<<<<<<<<<<<<\n");
        CookieStore temp = context.getCookieStore();
        if(temp!=null){
            for(Cookie cookie:temp.getCookies()){
                System.out.println("-----------------------------------------------------------");
                System.out.println( cookie.getName() + ":" + cookie.getValue() + ":" + cookie.getDomain() + ":" + cookie.getPath() + ":" + cookie.getExpiryDate());
                System.out.println("-----------------------------------------------------------");
            }
            return temp;
        }else{
            return null;
        }
    } catch (IOException e) {
      e.printStackTrace();
        return null;
    }
  }

  public static void main(String[] args) {
   DoubanLogin.Mylogin();
  }
}
